<?php
declare (strict_types = 1);

namespace app\controller\api\v1\expert\site;

use app\service\logic\api\site\SiteService;
use app\service\logic\common\tool\UploadService;
use app\service\logic\common\user\ExpertService;
use app\validate\common\site\SiteValidate;
use Exception;
use think\App;
use think\facade\Log;
use think\Request;
use app\BaseController;
use think\response\Json;

class SiteController extends BaseController
{
    protected $service;
    protected $userService;

    public function __construct(
        App $app,
        SiteService $service,
        ExpertService $userService
    ) {
        parent::__construct($app);

        $this->service = $service;
        $this->userService = $userService;
    }

    /**
     * 专家上传接口
     *
     * @return Json
     */
    public function upload()
    {
        $params = request()->only(['type']);
        $file = request()->file() ?? [];
        $this->validate($file, SiteValidate::class . '.file');
        $this->validate($params, SiteValidate::class . '.upload');

        $result = invoke(UploadService::class)->upload($params, $file);
        return amis([
            'value' => $result
        ]);
    }

    /**
     * 退出
     *
     * @return Json
     */
    public function logout()
    {
        try {
            $token = auth('expert')->token();
            $this->userService->deleteAccessTokenByToken($token);
        } catch (Exception $e) {
            Log::debug('expertLogoutException: ' . $e->getMessage());
        }
        return amis();
    }
}
